layui.use(['element','form','table','upload'],function(){
    var upload = layui.upload;
    var table = layui.table;
    var form = layui.form;

    var countNumber = '';

    table.render({
        elem: '#demo'
        ,toolbar: '#toolbarDemo'
        ,url: '/extAccountInformation/selectEntityPage' //数据接口
        ,page: true //开启分页
        ,where:{
            countNumber
        }
        ,cols: [[ //表头
            {field: 'ext_account_information_id',title: '产品编号',hide:true, sort: true}
            // ,{field: 'company_number', title: '公司代码', align: "center", templet: "#pro_pic",width:170 }
            ,{field: 'company_number',title: '公司代码', sort: true,width:100}
            ,{field: 'company_name',title: '公司名称', sort: true}
            ,{field: 'department_id', edit: 'text',title: '机构编号',width:120}
            ,{field: 'department_name', edit: 'text',title: '机构名称',width:200}
            ,{field: 'count_number', edit: 'text', title: '账户',width:120}
            ,{field: 'count_name', edit: 'text',title: '账户名称',width:120}
            ,{fixed: 'right',align:'center', toolbar: '#barDemo',title: '操作', width:80}
        ]]
        ,id : "ext_account_information"
    });

    //工具栏监听事件--删除
    table.on('tool(test)', function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        var id = obj.data.productId;
        if(layEvent === 'del'){
            layer.confirm('真的删除行么', function(index){
                obj.del();
                layer.close(index);
                console.log(data)
                //向服务端发送删除指令
                $.ajax({
                    url: '/copProduct/'+id,
                    type: 'DELETE',
                    dataType: 'json',
                    error: function (xhr, ajaxOptions, thrownError) {
                        if(xhr.status==403) {
                            alert("无权限")
                            location.reload([true])
                        }
                    }
                })
                    .done(function (result) {
                        if (result.code == 500) {
                            layer.msg('删除失败', {
                                icon: 1
                            })
                            location.reload([true])
                        }else{
                            layer.msg('删除成功', {
                                icon: 1
                            })
                        }
                    });
            });
        }
    });

    //监听单元格编辑--编辑
    table.on('edit(test)', function (obj) {
        var value = obj.value //得到修改后的值
            , data = obj.data //得到所在行所有键值
            , field = obj.field; //得到字段
        var a = {
            'productId': data.productId,
            'productName': data.productName,
            'productRetailPrice': data.productRetailPrice,
            'productSize':data.productSize,
            'productDescription':data.productDescription
        };
        var data = JSON.stringify(a)
        $.ajax({
            url: '/copProduct',
            type: 'PUT',
            headers: { 'Content-Type': 'application/json;charset=utf-8' },
            dataType: 'json',
            data: data,
            error: function (xhr, ajaxOptions, thrownError) {
                if(xhr.status==403) {
                    alert("无权限")
                    location.reload([true])
                }
            }
        })
            .done(function (result) {
                if (result.code == 500) {
                    layer.msg('修改失败', {
                        icon: 1
                    })
                    location.reload([true])
                }else{
                    alert("修改成功")
                    location.reload([true])
                }
            });
    });

    //头部工具栏监听事件--增加
    table.on('toolbar(test)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'add':
                window.location.href="extaccountAdd.html"
                break;
            case 'import':
                var display = document.getElementById('upload').getAttribute('display')
                if(display === '0'){
                    $('#upload').show();
                    $('#upload').attr('display','1');
                }else{
                    $('#upload').hide();
                    $('#upload').attr('display','0');
                }
                break;
        };
    });

    //搜索框事件
    var $ = layui.$, active = {
        reload: function(){
            var countNumber = document.getElementById('name').value;

            var table = layui.table;
            table.reload('ext_account_information', {
                where: {
                    countNumber
                }
            });
        }
    };

    $('.searchTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //多文件列表示例
    var demoListView = $('#demoList')
        ,uploadListIns = upload.render({
        elem: '#testList'
        ,url: '/copProduct/import' //改成您自己的上传接口
        ,accept: 'file'
        ,multiple: true
        ,auto: false
        ,bindAction: '#testListAction'
        ,choose: function(obj){
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件
            obj.preview(function(index, file, result){
                var tr = $(['<tr id="upload-'+ index +'">'
                    ,'<td>'+ file.name +'</td>'
                    ,'<td>'+ (file.size/1024).toFixed(1) +'kb</td>'
                    ,'<td>等待上传</td>'
                    ,'<td>'
                    ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                    ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                    ,'</td>'
                    ,'</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function(){
                    obj.upload(index, file);
                });

                //删除
                tr.find('.demo-delete').on('click', function(){
                    delete files[index]; //删除对应的文件
                    tr.remove();
                    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                });

                demoListView.append(tr);
            });
        }
        ,done: function(res, index, upload){
            if(res.code === '0'){ //上传成功
                var tr = demoListView.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                tds.eq(3).html(''); //清空操作

                var table = layui.table;
                table.reload('product', {
                    where: {
                        productName
                    }
                });

                return delete this.files[index]; //删除文件队列已经上传成功的文件
            }
            this.error(index, upload);
        }
        ,error: function(index, upload){
            var tr = demoListView.find('tr#upload-'+ index)
                ,tds = tr.children();
            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
        }
    });

});